/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.manager.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.genia.toolbox.basics.exception.technical.ExceptionTranslator;
import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.basics.exception.technical.TechnicalIOException;
import com.genia.toolbox.basics.manager.ExceptionManager;

/**
 * implementation of {@link ExceptionManager}.
 */
public class ExceptionManagerImpl
    implements ExceptionManager
{

  /**
   * the {@link ExceptionTranslator} indexed by the exception they handle.
   */
  private transient final Map<Class<? extends Exception>, ExceptionTranslator> translatersByClass = new HashMap<Class<? extends Exception>, ExceptionTranslator>();

  /**
   * the {@link List} of {@link ExceptionTranslator}.
   */
  private List<ExceptionTranslator> exceptionTranslaters;



  /**
   * convert an {@link Exception} to a {@link TechnicalException}.
   * 
   * @param exception
   *          the exception to convert
   * @return the converted exception
   * @see com.genia.toolbox.basics.manager.ExceptionManager#convertException(com.genia.toolbox.basics.exception.technical.TechnicalIOException)
   */
  public TechnicalException convertException(Exception exception)
  {
    ExceptionTranslator translater = translatersByClass.get(exception.getClass());
    if (translater == null) {
      for (ExceptionTranslator exceptionTranslater : getExceptionTranslaters()) {
        if (translater == null && exceptionTranslater.handle(exception.getClass())) {
          translater = exceptionTranslater;
        }
      }
      translatersByClass.put(exception.getClass(), translater);
    }
    return translater.convertException(exception);
  }



  /**
   * convert an {@link IOException} to a {@link TechnicalException}.
   * 
   * @param exception
   *          the exception to convert
   * @return the converted exception
   * @see com.genia.toolbox.basics.manager.ExceptionManager#convertException(java.io.IOException)
   */
  public TechnicalIOException convertException(IOException exception)
  {
    return (TechnicalIOException) convertException((Exception) exception);
  }



  /**
   * getter for the exceptionTranslaters property.
   * 
   * @return the exceptionTranslaters
   */
  public List<ExceptionTranslator> getExceptionTranslaters()
  {
    return exceptionTranslaters;
  }



  /**
   * setter for the exceptionTranslaters property.
   * 
   * @param exceptionTranslaters
   *          the exceptionTranslaters to set
   */
  public void setExceptionTranslaters(List<ExceptionTranslator> exceptionTranslaters)
  {
    this.exceptionTranslaters = exceptionTranslaters;
  }

}
